<?php

namespace app\api\controller\v2;

use addons\wechat\library\Config as ConfigService;
use app\admin\model\Kdniao;
use app\admin\model\litestore\Litestoregoods;
use app\admin\model\litestore\Litestorenews;
use app\admin\model\litestore\Litestoreorder;
use app\admin\model\Litestoretrack;
use app\admin\model\Profitorder;
use app\admin\model\Profitorderundo;
use app\admin\model\Profitvip;
use app\admin\model\Userrelation;
use app\admin\model\Usershare;
use app\admin\model\Userweixin;
use app\admin\model\Userwithdraw;
use app\api\service\ProfitService;
use app\common\controller\Api;
use app\common\model\UserGroup;
use EasyWeChat\Factory;
use fast\Http;
use OSS\OssClient;

/**
 *
 */
class User extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    protected $app = null;

    public function _initialize()
    {
        parent::_initialize();

        $config = [
            'app_id' => 'wx9ea4e568e37476b1',
            'secret' => '63c428341b5d9246544560df7345d76e',

            'response_type' => 'array',
            'log' => [
                'level' => 'debug',
            ],
        ];
        $this->app = Factory::miniProgram($config);
    }



    public function teamList()
    {

        $user = $this->auth->getUser();
        $page = $this->request->param('page');
        $keyword = $this->request->param('keyword');
        $uid  = $this->request->param('uid');
        $type  = $this->request->param('type');

        if($type == 'd'){
           $uid =  $user['id'];
        }

        $limit = 10;
        $offset = ($page - 1) * $limit;

        $where = array();
        $where['ref_uid'] = $uid;
        if ($keyword != '') {
            $where['username'] = array('like', '%' . $keyword . '%');
        }

        $userModel = new \app\admin\model\User();
        $list = $userModel->with(['relUser'])
            ->where($where)
            ->order(['createtime' => 'desc'])->limit($offset, $limit)
            ->select();
        foreach ($list as $key => $val) {
            if ($val['group_id'] == 1) {
                $list[$key]['group_name'] = 'U粉丝';
            } else if ($val['group_id'] == 2) {
                $list[$key]['group_name'] = '推荐官';
            } else if ($val['group_id'] == 3) {
                $list[$key]['group_name'] = '代言人';
            } else if ($val['group_id'] == 4) {
                $list[$key]['group_name'] = '合伙人';
            }
            $val['relUser']->hidden(['relation', 'refUid']);
            if (strpos($val['username'], "o") === 0) {
                $list[$key]['username'] = '未实名';
            }
            if($type == 't'){
                if($val['mobile'] != ''){
                    $list[$key]['mobile'] = substr_replace($val['mobile'], '****', 3, 4);;
                }
            }


        }

        $userRelation = new Userrelation();
        $relation = $userRelation->where('uid',$uid)->find();
        $ret = array();
        $ret['d_count'] = $relation['d_count'];
        $ret['d_one'] = $relation['d_one'];
        $ret['d_two'] = $relation['d_two'];
        $ret['d_three'] = $relation['d_three'];

        $ret['t_count'] = $relation['t_count'];
        $ret['t_one'] = $relation['t_one'];
        $ret['t_two'] = $relation['t_two'];
        $ret['t_three'] = $relation['t_three'];


        $refCount = $userModel->where('ref_uid',$uid)->count('id');
        if($refCount != $relation['d_count']){
            $ret['d_count'] = $refCount;
            $userRelation->where('uid',$uid)->update(['d_count'=>$refCount]);
        }


        $obj = array();
        $obj['list'] = $list;
        $obj['data'] = $ret;


        $this->success('', $obj);


    }



}
